package com.zziq.mianshi.manager;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RScript;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.IntegerCodec;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName CounterManager
 * @Description
 * @Created by Mr·张
 * @Date 2024/12/18 9:08
 **/
@Service
@Slf4j
public class CounterManager {
    @Resource
    private RedissonClient redissonClient;


    public long incrAndGetCounter(String key) {
        return incrAndGetCounter(key, 1, TimeUnit.SECONDS);
    }


    /** @Title: incrAndGetCounter
     * @Author: Mr·zziq
     * @Date: 2024-12-18 09:28:29
     * @Params: [key, timeInterval, timeUnit] 
     * @Return: long 
     * 
     * @Description: 
     */
    public long incrAndGetCounter(String key, int timeInterval, TimeUnit timeUnit) {
        int expireTimeSeconds;
        switch (timeUnit) {
            case SECONDS:
                expireTimeSeconds = timeInterval;
                break;
            case MINUTES:
                expireTimeSeconds = timeInterval*60;
                break;
            case HOURS:
                expireTimeSeconds = timeInterval*60*60;
                break;
            default:
                throw new IllegalArgumentException("不支持的时间单位: " + timeUnit);
        }
        return incrAndGetCounter(key, timeInterval, timeUnit, expireTimeSeconds);
    }


        /** @Title: incrAndGetCounter
         * @Author: Mr·zziq
         * @Date: 2024-12-18 09:23:05
         * @Params: [key, timeInterval, timeUnit, expireTime, expireTimeUnit]
         * @Return: long
         *
         * @Description:自增并计数
         */
    public long incrAndGetCounter(String key, int timeInterval, TimeUnit timeUnit,long expireTimeSeconds) {
        if(StrUtil.isBlank(key)){
            return 0;
        }
        long timeFactor;
        switch (timeUnit) {
            case SECONDS:
                timeFactor = Instant.now().getEpochSecond()/timeInterval;
                break;
            case MINUTES:
                timeFactor = Instant.now().getEpochSecond()/timeInterval/60;
                break;
            case HOURS:
                timeFactor = Instant.now().getEpochSecond()/timeInterval/60/60;
                break;
            default:
                throw new IllegalArgumentException("不支持的时间单位: " + timeUnit);
        }
        String redisKey = key + ":" + timeFactor;

        //lua脚本
        // Lua 脚本
        String luaScript =
                "if redis.call('exists', KEYS[1]) == 1 then " +
                        "  return redis.call('incr', KEYS[1]); " +
                        "else " +
                        "  redis.call('set', KEYS[1], 1); " +
                        "  redis.call('expire', KEYS[1], ARGV[1]); " +
                        "  return 1; " +
                        "end";
        RScript script = redissonClient.getScript(IntegerCodec.INSTANCE);
        Object countObj = script.eval(
                RScript.Mode.READ_WRITE,//脚本类型
                luaScript,//脚本对象
                RScript.ReturnType.INTEGER,//返回类型
                Collections.singletonList(redisKey),//key
                expireTimeSeconds
        );
        return (long) countObj;
    }
}
